<template>
  <div class="app-container">
    <h1>答辩评分系统系统配置</h1>
    <el-divider/>
    <el-button-group>
      <el-button type="primary" size="small" @click="UpdateYear = true">
        <svg-icon icon-class="update"/>
        修改当前学年
      </el-button>
      <el-button type="success" size="small" @click="UpdateTerm = true">
        <svg-icon icon-class="update"/>
        修改当前学期
      </el-button>
    </el-button-group>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      stripe
      border
      style="width: 100%">
      <el-table-column
        prop="configKey"
        label="配置项"/>
      <el-table-column
        prop="configValue"
        label="配置值"/>
    </el-table>

    <br>
    <div class="block">
      <el-pagination
        :current-page="page"
        :page-sizes="[5, 10, 15, 30, 50, 100]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="changSize"
        @current-change="changePage"/>
    </div>

    <el-dialog :close-on-click-modal="false" :visible.sync="UpdateYear" title="修改当前学年">
      <el-form :model="config_year">
        <el-form-item :label-width="LabelWidth" label="当前学年">
          <el-input-number v-model="config_year.configValue" :max="2030" :min="2020" label="当前学年"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="UpdateYear = false">取 消</el-button>
        <el-button type="primary" @click="HandleUpdateYear">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" :visible.sync="UpdateTerm" title="修改当前学期">
      <el-form :model="config_term">
        <el-form-item :label-width="LabelWidth" label="当前学期">
          <el-select v-model="config_term.configValue" placeholder="请选择当前学期">
            <el-option label="春季学期" value="春季学期"/>
            <el-option label="秋季学期" value="秋季学期"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="UpdateTerm = false">取 消</el-button>
        <el-button type="primary" @click="HandleUpdateTerm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getConfigs, updateConfig } from '@/api/admin/config'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      page: 1,
      total: 0,
      pageSize: 10,
      LabelWidth: '120px',
      UpdateYear: false,
      UpdateTerm: false,
      config_year: {
        id: null,
        configKey: '当前学年',
        configValue: ''
      },
      config_term: {
        id: null,
        configKey: '当前学期',
        configValue: ''
      }
    }
  },
  created() {
    this.getConfigList()
  },
  methods: {
    getConfigList() {
      this.listLoading = true
      getConfigs(this.page, this.pageSize).then(res => {
        this.listLoading = false
        this.total = Number.parseInt(res.result.counts)
        this.list = res.result.items
        for (let i = 0; i < this.list.length; i++) {
          if (this.list[i].configKey === this.config_year.configKey) {
            this.config_year.id = this.list[i].id
            this.config_year.configValue = this.list[i].configValue
          } else if (this.list[i].configKey === this.config_term.configKey) {
            this.config_term.id = this.list[i].id
            this.config_term.configValue = this.list[i].configValue
          }
        }
      }).catch(error => {
        console.log(error)
      })
    },
    changSize(pageSize) {
      this.pageSize = pageSize
      this.getConfigList()
    },
    changePage(page) {
      this.page = page
      this.getConfigList()
    },
    HandleUpdateYear() {
      this.UpdateYear = false
      updateConfig(this.config_year).then(res => {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.getConfigList()
      }).catch(error => {
        console.log(error)
      })
    },
    HandleUpdateTerm() {
      this.UpdateTerm = false
      updateConfig(this.config_term).then(res => {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.getConfigList()
      }).catch(error => {
        console.log(error)
      })
    }
  }
}
</script>
